﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SocialFront.Entities;
using SocialFront.Providers.Member;
using SocialFront.Providers.Relationship;

namespace SocialFront.Web.usercontrols.Membership
{
    public partial class MemberFanAndFriend : System.Web.UI.UserControl
    {
        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            int memberId = RelationshipProvider.Provider.GetMemberRelatedToCurrentNode()[0].Id;
            if (MemberProvider.Provider.GetCurrentMember() != null)
            {
                if (memberId !=
                    MemberProvider.Provider.GetCurrentMember().Id)
                {
                    bool isMemberFan = RelationshipProvider.Provider.IsMemberFan(memberId);
                    bool isMemberFriend = RelationshipProvider.Provider.IsMemberFriend(memberId);
                    if (isMemberFan || isMemberFriend)
                    {
                        if (isMemberFriend)
                        {
                            //Response.Write("Is Friend");
                            lbFanFriend.Enabled = false;
                            lbFanFriend.Text = "You're friends with this member";
                        }
                        else
                        {
                            lbFanFriend.Enabled = false;
                            lbFanFriend.Text = "You're a fan of this member";
                        }
                    }
                    else
                    {
                        lbFanFriend.Text = "Add Friend";
                    }
                }
            }
            else
                this.Visible = false;
        }

        /// <summary>
        /// Handles the Click event of the lbFanFriend control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void lbFanFriend_Click(object sender, EventArgs e)
        {
            int memberId = RelationshipProvider.Provider.GetMemberRelatedToCurrentNode()[0].Id;
            RelationshipProvider.Provider.CreateFansOrFriend(memberId);

            lbFanFriend.Enabled = false;

            bool isMemberFriend = RelationshipProvider.Provider.IsMemberFriend(memberId);

            if (isMemberFriend)
            {
                lbFanFriend.Text = "You're a friend of this member";
            }
            else
            {
                lbFanFriend.Text = "You're a fan of this member";
            }
        }
    }
}